package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;



    /*
        添加到购物车
        添加套餐到购物车
        POST http://localhost:8080/shoppingCart/add
           {"amount":50,"setmealId":"1586616156652236802","name":"架构师套餐","image":"2ba69fb6b4f14b4fac6146b9209670a1.png"}

    添加某道菜到购物车
    post http://localhost:8080/shoppingCart/add
         {"amount":20,"dishFlavor":"微辣","dishId":"1397854865672679425","name":"鱼香炒鸡蛋","image":"0f252364-a561-4e8d-8065-9a6797a6b1d3.jpg"}

select *  from shopping_cart where user_id='xxxx' and dish_id='disID'
select *  from shopping_cart where user_id='xxxx' and setmeal_id='setmealid'


    */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody  ShoppingCart shoppingCart){
        log.info("购物车数据:{}",shoppingCart);
        //设置用户id，指定当前是哪个用户的购物车数据
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,currentId);
        //通过判断dishId是否有值决定本次是套餐还是菜品
        if(dishId!=null){
            //添加到购物车的是菜品
            //select *  from shopping_cart where user_id='xxxx' and dish_id='disID'
            queryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else{
            //select *  from shopping_cart where user_id='xxxx' and setmeal_id='setmealid'
            //添加到购物车的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        //查询当前菜品或者套餐是否在购物车中
        //select *  from shopping_cart where user_id='xxxx' and dish_id='disID'
        //select *  from shopping_cart where user_id='xxxx' and setmeal_id='setmealid'
        ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);
        if(cartServiceOne!=null){
            //如果已经存在，就在原来数量基础上加一
            Integer number = cartServiceOne.getNumber();
            cartServiceOne.setNumber(number+1);
            shoppingCartService.updateById(cartServiceOne);
        }else{
            //如果不存在，则添加到购物车，数量默认就是1
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            cartServiceOne=shoppingCart;
        }

        return R.success(cartServiceOne);
    }


    //查看购物车
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        System.out.println("查看购物车");
        //SELECT * FROM shopping_cart WHERE user_id='1599598913036627970' ORDER BY create_time;
        LambdaQueryWrapper<ShoppingCart> lqw=new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId()); //WHERE user_id='1599598913036627970'
        lqw.orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(lqw);
        return R.success(list);
    }


    //*_清空购物车
    //DELETE  http://localhost:8080/shoppingCart/clean
    //DELETE  FROM shopping_cart  WHERE user_id=''
    @DeleteMapping("/clean")
    public R<String> clean(){
        //DELETE  FROM shopping_cart  WHERE user_id=''
        LambdaQueryWrapper<ShoppingCart> lqw=new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId()); //WHERE user_id='1599598913036627970'
        shoppingCartService.remove(lqw);
        return R.success("清空购物车成功");
    }


 }  